from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

db = SQLAlchemy()

class User(db.Model):
    """用户模型"""
    __tablename__ = 'users'
    
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), nullable=False)
    gender = db.Column(db.String(10))  # 性别：male/female
    grade = db.Column(db.String(20))   # 年级
    avatar = db.Column(db.String(200)) # 头像URL
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'gender': self.gender,
            'grade': self.grade,
            'avatar': self.avatar,
            'created_at': self.created_at.isoformat()
        }

class Settings(db.Model):
    """用户设置模型"""
    __tablename__ = 'settings'
    
    id = db.Column(db.Integer, primary_key=True)
    api_key = db.Column(db.String(100))
    api_model = db.Column(db.String(50), default='deepseek-chat')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    def to_dict(self):
        return {
            'id': self.id,
            'api_key': self.api_key,
            'api_model': self.api_model,
            'updated_at': self.updated_at.isoformat()
        } 